package com.shelpe.services.simbasync.controller;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shelpe.services.simbasync.service.simbaproxy.DiamondShowProxy;

@RestController
@RequestMapping(value="/simbasync")
public class AdZoneShowController {
	
	@Autowired
	private DiamondShowProxy adzoneProxy;
	
	@RequestMapping("/tenants/{user}/adzone")
	@ResponseBody
	public String getAdZoneInfo(@PathVariable("user") String user){
		
		boolean isNumber = user.matches("-?[0-9]+.*[0-9]*");
		
		return adzoneProxy.getAdZoneInfo(Long.valueOf(user));
	}
	
	@RequestMapping("/tenants/{user}/adzone/{adzone_id}")
	@ResponseBody
	public String getRptAdZone(@PathVariable("user") String user, @PathVariable("adzone_id") Long adZoneId){
		
		return adzoneProxy.getRptAdZone(Long.valueOf(user), adZoneId);
	}
	
	@RequestMapping("/tenants/{user}/adcampaign")
	@ResponseBody
	public String getRptAdvertiserCampaign(@PathVariable("user") String user, @RequestParam("start_time") String startTime, 
			@RequestParam("end_time") String endTime, @RequestParam("rpt_type") String rptType){
		
		return adzoneProxy.getRptAdvertiserCampaign(Long.valueOf(user), startTime, endTime, rptType);
	}
	
	@RequestMapping("/tenants/{user}/adtrans")
	@ResponseBody
	public String getRptAdvertiserTrans(@PathVariable("user") String user, @RequestParam("start_time") String startTime, 
			@RequestParam("end_time") String endTime, @RequestParam("rpt_type") String rptType){
		
		return adzoneProxy.getRptAdvertiserTrans(Long.valueOf(user), startTime, endTime, rptType);
	}
	
	@RequestMapping("/tenants/{user}/rptadzone")
	@ResponseBody
	public String getRptAdvertiserAdzone(@PathVariable("user") String user, @RequestParam("start_time") String startTime, 
			@RequestParam("end_time") String endTime, @RequestParam("rpt_type") String rptType){
		
		return adzoneProxy.getRptAdvertiserAdzone(Long.valueOf(user), startTime, endTime, rptType);
	}
	
	@RequestMapping("/tenants/{user}/adkeyword")
	@ResponseBody
	public String getRptAdvertiserKeyword(@PathVariable("user") String user, @RequestParam("start_time") String startTime, 
			@RequestParam("end_time") String endTime){
		
		return adzoneProxy.getRptAdvertiserKeyword(Long.valueOf(user), startTime, endTime);
	}
	
	@RequestMapping("/tenants/{user}/adaccount")
	@ResponseBody
	public String getRptAdvertiserAccount(@PathVariable("user") String user, @RequestParam("start_time") String startTime, 
			@RequestParam("end_time") String endTime, @RequestParam("rpt_type") String rptType){
		
		return adzoneProxy.getRptAdvertiserAccount(Long.valueOf(user), startTime, endTime, rptType);
	}
	
	@RequestMapping("/tenants/{user}/adcategory")
	@ResponseBody
	public String getRptAdvertiserCategory(@PathVariable("user") String user, @RequestParam("start_time") String startTime, 
			@RequestParam("end_time") String endTime){
		
		return adzoneProxy.getRptAdvertiserCategory(Long.valueOf(user), startTime, endTime);
	}
	
	@RequestMapping("/tenants/{user}/adtask")
	@ResponseBody
	public String getRptTask(@PathVariable("user") String user, @RequestParam("task_id") Long taskId){
		
		return adzoneProxy.getRptTask(Long.valueOf(user), taskId);
	}
	
	@RequestMapping("/tenants/{user}/adboard")
	@ResponseBody
	public String getRptAdvertiserAdboard(@PathVariable("user") String user, @RequestParam("start_time") String startTime, 
			@RequestParam("end_time") String endTime, @RequestParam("rpt_type") String rptType){
		
		return adzoneProxy.getRptAdvertiserAdboard(Long.valueOf(user), startTime, endTime, rptType);
	}
	
	@RequestMapping("/tenants/{user}/addest")
	@ResponseBody
	public String getRptAdvertiserDest(@PathVariable("user") String user, @RequestParam("start_time") String startTime, 
			@RequestParam("end_time") String endTime, @RequestParam("rpt_type") String rptType){
		
		return adzoneProxy.getRptAdvertiserDest(Long.valueOf(user), startTime, endTime, rptType);
	}
	
	/***********************辅助函数******************************/

	public boolean getRemoteFile(String strUrl, String fileName) throws IOException { 
		
		URL url = new URL(strUrl); 
		HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
		DataInputStream input = new DataInputStream(conn.getInputStream()); 
		DataOutputStream output = new DataOutputStream(new FileOutputStream(fileName)); 
		byte[] buffer = new byte[1024 * 8]; 
		int count = 0; 
		
		while ((count = input.read(buffer)) > 0) { 
			output.write(buffer, 0, count); 
		} 
		output.close(); 
		input.close(); 
		return true; 
	}

}
